#!/usr/bin/liquidsoap
# -*- mode: Ruby; coding: utf-8 -*-

%include "passwords.liq"

set("log.file.path", "#{root}/logs/liquidsoap.log")
set("log.stdout", true)
set("server.telnet", true)
set("harbor.port", 8080)

##
## Paths
##
music = "#{root}/music"
scripts = "#{root}/liquidsoap/scripts"

##
## AE2 startup/shutdown notifications
##
def update_ae2(artist, title,
               ogg="http://ae-streaming.utbm.fr/flux.ogg",
               mp3="http://ae-streaming.utbm.fr/flux.mp3")
  system("#{scripts}/update-ae2.py #{ae2_api_key} "
         ^"ogg #{quote(ogg)} "
         ^"mp3 #{quote(mp3)} "
         ^"title #{quote(title)} "
         ^"artist #{quote(artist)} &")
end

def halt_ae()
  update_ae2("", "", "", "")
end

on_shutdown(halt_ae)
update_ae2("Superflux", "Démarrage en cours...")

##
## Utility functions
##

# Given the stream metadata, run external feedback scripts.
def feedback_metadata(m)
  update_ae2(m['artist'], m['title'])
end

# Ask Streamux if a DJ user/password is okay for live connection.
def dj_auth(user, password)
  ret = get_process_lines("#{scripts}/dj-authnz.py"
                          ^" #{quote(ae2_api_key)}"
                          ^" #{quote(user)}"
                          ^" #{quote(password)}")
  ret = list.hd(ret)
  bool_of_string(ret)
end

##
## Okay, let's build this stream.
##

# If everything epic-fails, play 4"33 in a loop.
epicfail = single(id="epicfail", "#{music}/specials/silence.ogg")

# AutoDJ is the automated playback system plugged into streamux
def request()
  request.create(get_process_output("#{scripts}/autodj-next.py"))
end
autodj = request.dynamic(id="autodj", request)

# Live DJ music queue (will be managed by streamux eventually).
queue = request.equeue(id="queue")

# Track-sensitively switch between AutoDJ and manual feeding, with
# crossfading.
radio = fallback([queue, autodj])
radio = smart_crossfade(conservative=true, radio)

# Live DJ voice input from harbor.
voice = mic_filter(input.harbor(id="live_voice", auth=dj_auth, "voice"))

# The above two get mixed together with the fancy "Lower music volume
# when DJ is talking" operator.
radio = smooth_add(normal=radio, special=voice)
radio = nrj(radio)

# Complete live input, superseding the smartish mixed stuff above.
full_live = input.harbor(id="live", auth=dj_auth, "live")

# The full live supersedes this processed input
radio = fallback(track_sensitive=false, [full_live, radio, epicfail])

# Feed metadata info to outside listeners, on the ogg stream only (our
# canonical stream)
radio_ogg = on_metadata(feedback_metadata, radio)

# Aaand output.
output.icecast.vorbis.abr(id="ogg", mount=icecast_mount_ogg,
                          description="Superflux (ogg)",
                          url="http://ae.utbm.fr/superflux", public=true,
                          port=icecast_port, host=icecast_host,
                          password=icecast_password, restart=true, radio_ogg)

output.icecast.mp3(id="mp3", mount=icecast_mount_mp3,
                   description="Superflux (mp3)",
                   url="http://ae.utbm.fr/superflux", public=true,
                   port=icecast_port, host=icecast_host,
                   password=icecast_password, restart=true, radio)
